import { 
  Tenant, 
  Organization, 
  User, 
  Role, 
  Permission, 
  Department, 
  Position,
  SubscriptionPlan
} from '@/types';

// 工具函数
export const generateId = () => `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;

export const getRandomElement = <T>(array: T[]): T => {
  return array[Math.floor(Math.random() * array.length)];
};

export const getRandomElements = <T>(array: T[], count: number): T[] => {
  const shuffled = [...array].sort(() => 0.5 - Math.random());
  return shuffled.slice(0, count);
};

export const getRandomDate = (start: Date, end: Date): string => {
  const startTime = start.getTime();
  const endTime = end.getTime();
  const randomTime = startTime + Math.random() * (endTime - startTime);
  return new Date(randomTime).toISOString();
};

// 权限数据
export const generatePermissions = (): Permission[] => [
  // 仪表板权限
  { id: '1', code: 'dashboard.view', name: '查看仪表板', module: 'dashboard', action: 'view', resource: 'dashboard', type: 'menu', parentId: undefined, level: 1, path: '1', sort: 1, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  
  // 租户管理权限
  { id: '2', code: 'tenant.view', name: '查看租户', module: 'tenant', action: 'view', resource: 'tenant', type: 'menu', parentId: undefined, level: 1, path: '2', sort: 2, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '3', code: 'tenant.create', name: '创建租户', module: 'tenant', action: 'create', resource: 'tenant', type: 'button', parentId: '2', level: 2, path: '2/3', sort: 1, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '4', code: 'tenant.edit', name: '编辑租户', module: 'tenant', action: 'edit', resource: 'tenant', type: 'button', parentId: '2', level: 2, path: '2/4', sort: 2, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '5', code: 'tenant.delete', name: '删除租户', module: 'tenant', action: 'delete', resource: 'tenant', type: 'button', parentId: '2', level: 2, path: '2/5', sort: 3, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '6', code: 'tenant.manage', name: '管理租户', module: 'tenant', action: 'manage', resource: 'tenant', type: 'api', parentId: '2', level: 2, path: '2/6', sort: 4, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  
  // 组织管理权限
  { id: '7', code: 'organization.view', name: '查看组织', module: 'organization', action: 'view', resource: 'organization', type: 'menu', parentId: undefined, level: 1, path: '7', sort: 3, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '8', code: 'organization.create', name: '创建组织', module: 'organization', action: 'create', resource: 'organization', type: 'button', parentId: '7', level: 2, path: '7/8', sort: 1, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '9', code: 'organization.edit', name: '编辑组织', module: 'organization', action: 'edit', resource: 'organization', type: 'button', parentId: '7', level: 2, path: '7/9', sort: 2, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '10', code: 'organization.delete', name: '删除组织', module: 'organization', action: 'delete', resource: 'organization', type: 'button', parentId: '7', level: 2, path: '7/10', sort: 3, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '11', code: 'organization.manage', name: '管理组织', module: 'organization', action: 'manage', resource: 'organization', type: 'api', parentId: '7', level: 2, path: '7/11', sort: 4, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  
  // 用户管理权限
  { id: '12', code: 'user.view', name: '查看用户', module: 'user', action: 'view', resource: 'user', type: 'menu', parentId: undefined, level: 1, path: '12', sort: 4, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '13', code: 'user.create', name: '创建用户', module: 'user', action: 'create', resource: 'user', type: 'button', parentId: '12', level: 2, path: '12/13', sort: 1, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '14', code: 'user.edit', name: '编辑用户', module: 'user', action: 'edit', resource: 'user', type: 'button', parentId: '12', level: 2, path: '12/14', sort: 2, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '15', code: 'user.delete', name: '删除用户', module: 'user', action: 'delete', resource: 'user', type: 'button', parentId: '12', level: 2, path: '12/15', sort: 3, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '16', code: 'user.manage', name: '管理用户', module: 'user', action: 'manage', resource: 'user', type: 'api', parentId: '12', level: 2, path: '12/16', sort: 4, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  
  // 角色管理权限
  { id: '17', code: 'role.view', name: '查看角色', module: 'role', action: 'view', resource: 'role', type: 'menu', parentId: undefined, level: 1, path: '17', sort: 5, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '18', code: 'role.create', name: '创建角色', module: 'role', action: 'create', resource: 'role', type: 'button', parentId: '17', level: 2, path: '17/18', sort: 1, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '19', code: 'role.edit', name: '编辑角色', module: 'role', action: 'edit', resource: 'role', type: 'button', parentId: '17', level: 2, path: '17/19', sort: 2, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '20', code: 'role.delete', name: '删除角色', module: 'role', action: 'delete', resource: 'role', type: 'button', parentId: '17', level: 2, path: '17/20', sort: 3, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '21', code: 'role.manage', name: '管理角色', module: 'role', action: 'manage', resource: 'role', type: 'api', parentId: '17', level: 2, path: '17/21', sort: 4, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  
  // 权限管理权限
  { id: '22', code: 'permission.view', name: '查看权限', module: 'permission', action: 'view', resource: 'permission', type: 'menu', parentId: undefined, level: 1, path: '22', sort: 6, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '23', code: 'permission.manage', name: '管理权限', module: 'permission', action: 'manage', resource: 'permission', type: 'api', parentId: '22', level: 2, path: '22/23', sort: 1, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  
  // 系统设置权限
  { id: '24', code: 'system.view', name: '查看系统设置', module: 'system', action: 'view', resource: 'system', type: 'menu', parentId: undefined, level: 1, path: '24', sort: 9, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
  { id: '25', code: 'system.manage', name: '管理系统设置', module: 'system', action: 'manage', resource: 'system', type: 'api', parentId: '24', level: 2, path: '24/25', sort: 1, isSystem: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
];

// 角色数据
export const generateRoles = (): Role[] => [
  {
    id: '1',
    tenantId: '1',
    name: '超级管理员',
    code: 'super_admin',
    description: '系统超级管理员，拥有所有权限',
    permissions: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25'],
    isSystem: true,
    isDefault: false,
    status: 'active',
    sort: 1,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  },
  {
    id: '2',
    tenantId: '1',
    name: '租户管理员',
    code: 'tenant_admin',
    description: '租户管理员，管理租户内的所有资源',
    permissions: ['1', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '24', '25'],
    isSystem: true,
    isDefault: true,
    status: 'active',
    sort: 2,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  },
  {
    id: '3',
    tenantId: '1',
    name: '组织管理员',
    code: 'org_admin',
    description: '组织管理员，管理组织内的用户和部门',
    permissions: ['1', '12', '13', '14', '16'],
    isSystem: true,
    isDefault: false,
    status: 'active',
    sort: 3,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  },
  {
    id: '4',
    tenantId: '1',
    name: '普通用户',
    code: 'user',
    description: '普通用户，只能查看基本信息',
    permissions: ['1', '12'],
    isSystem: true,
    isDefault: true,
    status: 'active',
    sort: 4,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  },
];